/*
 * Created on Jan 1, 2008
 * 
 * By Erick Audet for OTN
 *
 * Copyright (c) 2008 Oracle Corporation. All Rights Reserved.
 *
 *
 */

package com.sample.utils;

import java.io.File;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.Transaction;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.StoreConfig;

public class MyDbEnv {

    private static Environment myEnv;
    private static EntityStore store;
    private static MyDbEnv myDbEnv;
    private final static File myDbEnvPath = new File("D:/sample/");

    // Our constructor does nothing
    private MyDbEnv() {}
    
    public static MyDbEnv getInstance(){
    	if (myDbEnv == null){
    		myDbEnv = new MyDbEnv();
    		try {
				setup(myDbEnvPath, false);
			} catch (DatabaseException e) {
				e.printStackTrace();
			}
    	}
    	return myDbEnv;
    }

    // The setup() method opens the environment and store
    // for us.
    private static void setup(File envHome, boolean readOnly) 
        throws DatabaseException {

        EnvironmentConfig myEnvConfig = new EnvironmentConfig();
        StoreConfig storeConfig = new StoreConfig();

        myEnvConfig.setReadOnly(readOnly);
        storeConfig.setReadOnly(readOnly);

        // If the environment is opened for write, then we want to be 
        // able to create the environment and entity store if 
        // they do not exist.
        myEnvConfig.setAllowCreate(!readOnly);
        storeConfig.setAllowCreate(!readOnly);

        // Allow transactions if we are writing to the store.
        myEnvConfig.setTransactional(!readOnly);
        storeConfig.setTransactional(!readOnly);

        // Open the environment and entity store
        myEnv = new Environment(envHome, myEnvConfig);
        store = new EntityStore(myEnv, "EntityStore", storeConfig);

    }

    // Return a handle to the entity store
    public EntityStore getEntityStore() {
        return store;
    }

    // Return a handle to the environment
    private Environment getEnv() {
        return myEnv;
    }
    
    public Transaction beginTransaction(){
    	try {
			return myEnv.beginTransaction(null, null);
		} catch (DatabaseException e) {
			e.printStackTrace();
		}
		
		return null;
    }



    // Close the store and environment
    private void close() {
        if (store != null) {
            try {
                store.close();
            } catch(DatabaseException dbe) {
                System.err.println("Error closing store: " + 
                                    dbe.toString());
               System.exit(-1);
            }
        }

        if (myEnv != null) {
            try {
                // Finally, close the store and environment.
                myEnv.close();
            } catch(DatabaseException dbe) {
                System.err.println("Error closing MyDbEnv: " + 
                                    dbe.toString());
               System.exit(-1);
            }
        }
    }
}

